// ==UserScript==
// @name           Gladiatus Helper
// @description    Gladiatus Helper
// @autor          作者：Jerrico
// @include        *.gladiatus.*
// @exclude        *board.gladiatus.*
// @version        0.1
// ==/UserScript==

var gameServer = top.location.host;
var current_url = window.location.href;
var sh = document.getElementById('header_values_general').getElementsByTagName('a')[0].getAttribute('href');
sh = sh.substring(sh.indexOf("sh="),sh.length);
current_url = (current_url.indexOf("sh=") == -1)?current_url.substring(current_url.indexOf("index.php?")+9,current_url.length):current_url.substring(current_url.indexOf("index.php?")+9,current_url.indexOf("sh="));

if(true) show_quickbar();
// 快速聯結
function show_quickbar(){ // This script was made by EnigmaBrand, modified by Jerrico
﻿  unsafeWindow.places = (localStorage.getItem(gameServer+".quickbar") != null) ? localStorage.getItem(gameServer+".quickbar") : "0";
﻿  var opened=0;

﻿  // Create my div and append it to the body tag
﻿  vquickbar = document.createElement("div");
﻿  vquickbar.setAttribute("id", "quickbar");
﻿  var body = document.getElementsByTagName("body");
﻿  body[0].appendChild(vquickbar);

﻿  // this function shows the tab when you mouseover it
﻿  unsafeWindow.showquick = function() {
﻿  ﻿  if(document.getElementById("quickbar").style.left == "-252px") {
﻿  ﻿  ﻿  document.getElementById("quickbar").style.left = "0px;"
﻿  ﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  ﻿  }
﻿  ﻿  if(opened==0) {
﻿  ﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  ﻿  ﻿  opened=1;
﻿  ﻿  }
﻿  }

﻿  // this function closes the tab
﻿  unsafeWindow.hidequick = function() {
﻿  ﻿  document.getElementById("quickbar").style.left = "-252px;"
﻿  }

﻿  // this function deletes a quickmark
﻿  unsafeWindow.qbdeleteentry = function(deleteme) {
﻿  ﻿  deleteme = deleteme.slice(0,-1);
﻿  ﻿  var splitplaces = unsafeWindow.places.split(/;/);
﻿  ﻿  for(i = 0; i < splitplaces.length-1; i++){
﻿  ﻿  ﻿  if(splitplaces[i] == deleteme){
﻿  ﻿  ﻿  ﻿  splitplaces.splice(i,1);
﻿  ﻿  ﻿  ﻿  break;
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  if(splitplaces.length-1 > 0) unsafeWindow.places = splitplaces.join(';');
﻿  ﻿  else unsafeWindow.places = "0";
﻿  ﻿  localStorage.setItem(gameServer+".quickbar", unsafeWindow.places)
﻿  ﻿  //window.setTimeout(localStorage.setItem, 0, gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  }

﻿  // this function renames one of your quickmarks
﻿  unsafeWindow.qbrenameentry = function(renameme) {
﻿  ﻿  var renameto = prompt("重新命名捷徑:","");
﻿  ﻿  if((renameto == '') || (renameto == '^') || (renameto == ';')) {
﻿  ﻿  ﻿  alert("無效名稱! 不能空白或有 ^ 或 ; 符號!");
﻿  ﻿  ﻿  return;
﻿  ﻿  }
﻿  ﻿  renameme = renameme.slice(0,-1);
﻿  ﻿  var splitplaces = unsafeWindow.places.split(/;/);
﻿  ﻿  var splitpiece = '';
﻿  ﻿  for(i = 0; i < splitplaces.length-1; i++){
﻿  ﻿  ﻿  if(splitplaces[i] == renameme) {
﻿  ﻿  ﻿  ﻿  splitpiece = splitplaces[i].split('|');
﻿  ﻿  ﻿  ﻿  splitplaces[i] = renameto + '|' + splitpiece[1];
﻿  ﻿  ﻿  ﻿  break;
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  if(splitplaces.length-1 > 0) unsafeWindow.places = splitplaces.join(';');

﻿  ﻿  localStorage.setItem(gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  //window.setTimeout(localStorage.setItem, 0, gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  }

﻿  // this function adds a new entry to your quickmark list
﻿  unsafeWindow.qbaddentry = function() {
﻿  ﻿  
﻿  ﻿  var addname = prompt("你想要取什麼名字?","");
﻿  ﻿  if((addname.search(/\|/) != -1) || (addname.search(/;/) != -1)) {
﻿  ﻿  ﻿  alert('無效名稱! 不能空白或有 | 或 ; 符號!');
﻿  ﻿  ﻿  exit;
﻿  ﻿  }
﻿  ﻿  var addlocation = window.location.href;
﻿  ﻿  if(addlocation.indexOf('sh=') != -1) addlocation = addlocation.substring(0,addlocation.indexOf('sh='));
﻿  ﻿  while(addname.indexOf('null') != -1) {
﻿  ﻿  ﻿  addname = addname.replace('null','');
﻿  ﻿  }
﻿  ﻿  if(unsafeWindow.places != "0") unsafeWindow.places += addname + '|' + addlocation + ';';
﻿  ﻿  else unsafeWindow.places = addname + '|' + addlocation + ';';
﻿  ﻿  localStorage.setItem(gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  //window.setTimeout(localStorage.setItem, 0, gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  }

﻿  // this function deletes all of your quickmarks
﻿  unsafeWindow.qbdeleteall = function() {
﻿  ﻿  if(confirm("你確定要刪除全部的捷徑?")) {
﻿  ﻿  ﻿  unsafeWindow.places = "0";
﻿  ﻿  ﻿  localStorage.setItem(gameServer+".quickbar", "0");
﻿  ﻿  ﻿  //window.setTimeout(localStorage.setItem, 0, gameServer+".quickbar", "0");﻿  ﻿  
﻿  ﻿  }
﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  }

﻿  // this function moves one of your quickmarks down 1 in the list
﻿  unsafeWindow.qbmovedown = function(moveitdown) {
﻿  ﻿  moveitdown = moveitdown.slice(0,-1);
﻿  ﻿  var splitplaces = unsafeWindow.places.split(/;/);
﻿  ﻿  for(i = 0; i < splitplaces.length-1; i++){
﻿  ﻿  ﻿  if(splitplaces[i] == moveitdown) {
﻿  ﻿  ﻿  ﻿  if(i<splitplaces.length-1) {
﻿  ﻿  ﻿  ﻿  ﻿  splitplaces[i] = splitplaces[i+1];
﻿  ﻿  ﻿  ﻿  ﻿  splitplaces[i+1] = moveitdown;
﻿  ﻿  ﻿  ﻿  ﻿  break;
﻿  ﻿  ﻿  ﻿  } else {
﻿  ﻿  ﻿  ﻿  ﻿  alert("這個捷徑已經是最後一個了.");
﻿  ﻿  ﻿  ﻿  ﻿  break;
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  unsafeWindow.places = splitplaces.join(';');
﻿  ﻿  localStorage.setItem(gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  //window.setTimeout(localStorage.setItem, 0, gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  }


﻿  // this function moves one of your quickmarks up 1 in the list
﻿  unsafeWindow.qbmoveup = function(moveitup) {
﻿  ﻿  moveitup = moveitup.slice(0,-1);
﻿  ﻿  var splitplaces = unsafeWindow.places.split(/;/);
﻿  ﻿  var moveitdown = '';
﻿  ﻿  for(i = 0; i < splitplaces.length-1; i++){
﻿  ﻿  ﻿  if(splitplaces[i] == moveitup) {
﻿  ﻿  ﻿  ﻿  if(i<splitplaces.length-1) {
﻿  ﻿  ﻿  ﻿  ﻿  splitplaces[i] = splitplaces[i-1];
﻿  ﻿  ﻿  ﻿  ﻿  splitplaces[i-1] = moveitup;
﻿  ﻿  ﻿  ﻿  ﻿  break;
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  ﻿  else {
﻿  ﻿  ﻿  ﻿  ﻿  alert("這個捷徑已經是第一個了.");
﻿  ﻿  ﻿  ﻿  ﻿  break;
﻿  ﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  ﻿  unsafeWindow.places = splitplaces.join(';');
﻿  ﻿  localStorage.setItem(gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  //window.setTimeout(localStorage.setItem, 0, gameServer+".quickbar", unsafeWindow.places);
﻿  ﻿  unsafeWindow.qbbuildinterior();
﻿  }

﻿  // this function builds the innerHTML of the tab without a page reload
﻿  unsafeWindow.qbbuildinterior = function()
﻿  {
﻿  ﻿  var qbHTML = '<div id="quicktab" onmouseover="javascript:showquick()" onclick="javascript:hidequick()">連結</div>';
﻿  ﻿  qbHTML += '<div id="qhead"><a style="border-bottom:1px #542C0F dotted; color: #542C0F;" href="http://userscripts.org/users/55264/scripts" title="本script修改自Ikiariam Quickbar v1.6">本script修改自IQ v1.6</a></div>';
﻿  ﻿  qbHTML += '<div id="qplaces">';

﻿  ﻿  // Build the places list
﻿  ﻿  if(unsafeWindow.places != "0") {
﻿  ﻿  ﻿  qbHTML += '<div style="border-bottom:1px #F3DDB5 solid; background: #FDF7DD; width:266px; margin-top:3px; height:265px; overflow-y:auto; overflow-x:hidden;"><table style="width:265px; border: none;">';
﻿  ﻿  ﻿  var placepiece = '';
﻿  ﻿  ﻿  var splitplaces = unsafeWindow.places.split(";");
﻿  ﻿  ﻿  for(i = 0; i < splitplaces.length-1; i++){
﻿  ﻿  ﻿  ﻿  placepiece = splitplaces[i].split("|");
﻿  ﻿  ﻿  ﻿  qbHTML += '<tr>';
﻿  ﻿  ﻿  ﻿  qbHTML += '<td width="32%" align="right" valign="bottom">';
﻿  ﻿  ﻿  ﻿  if(i>0) qbHTML += '<a title="上移" href="javascript:qbmoveup(\'' + placepiece[0] + '|' + placepiece[1] + ';\');">(↑)</a>';

﻿  ﻿  ﻿  ﻿  if(i<splitplaces.length-2) qbHTML += '<a title="下移" href="javascript:qbmovedown(\'' + placepiece[0] + '|' + placepiece[1] + ';\');">(↓)</a>';

﻿  ﻿  ﻿  ﻿  qbHTML += '<a title="刪除捷徑" href="javascript:qbdeleteentry(\'' + placepiece[0] + '|' + placepiece[1] + ';\');">(X)</a>';
﻿  ﻿  ﻿  ﻿  qbHTML += '<a title="變更捷徑" href="javascript:qbrenameentry(\'' + placepiece[0] + '|' + placepiece[1] + ';\');">(R)</a> - </td>';
﻿  ﻿  ﻿  ﻿  qbHTML += '<td align="left" width="68%" valign="bottom">&nbsp;<a href="' + placepiece[1] + sh + '">' + placepiece[0] + '</a></td>';
﻿  ﻿  ﻿  ﻿  qbHTML += '</tr>';
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  qbHTML += '</table></div>';
﻿  ﻿  }
﻿  ﻿  else qbHTML += '<div style="border-bottom:1px #F3DDB5 solid; width:266px; height:265px; overflow-y:auto; overflow-x:hidden;"><table style="width:265px; border: none;"><tr><td align="center">沒有任何資料</td></tr></table></div>';

﻿  ﻿  qbHTML += '<font size=2><a href="javascript:qbaddentry();">將此頁面加入捷徑</a>　　<a href="javascript:qbdeleteall();">刪除全部捷徑</a></font></div>';
﻿  ﻿  qbHTML += '<div id="qfoot"></div>';
﻿  ﻿  document.getElementById("quickbar").innerHTML = qbHTML;
﻿  }

﻿  // Add the style
﻿  GM_addStyle("#quickbar { width:300px; position:fixed; left:-302px; height:350px; top:215px; z-index: 500; background: #FDF7DD; background-repeat:repeat-y; border:1px black solid;}");
﻿  GM_addStyle("#qhead { height:30px; width:300px; position:absolute; left:0px; top:0px; background: #FDF7DD; line-height:38px; font-weight:bold; font-size:11px;} ");
﻿  GM_addStyle("#quickbar:hover { left:0px; }");
﻿  GM_addStyle("#qfoot { width:300px; height:3px; background: #FDF7DD; position:absolute; bottom:0px; left:0px;}");
﻿  GM_addStyle("#quicktab { z-index:496; background: #FDF7DD; width:26px; height:100px; position:absolute; right:-26px; top:0px; border:1px black solid; } ");
﻿  GM_addStyle("#quicktab:hover { cursor: pointer; } ");
﻿  GM_addStyle("#qplaces { position:absolute; top:30px; bottom:3px; left:15px; overflow:auto; } ");

﻿  document.getElementById("quickbar").innerHTML = '<div id="quicktab" onmouseover="showquick()" onclick="hidequick()">連結</div>';
}

autoFillAuctionFields();
// 自動填入拍賣商品之最低金額
function autoFillAuctionFields(){
﻿  //Elenco tutti i valori degli oggetti all'asta
﻿  var divs = XQuery(".//div[@class='auction_bid_div']");
﻿  if(divs.snapshotLength) {
﻿  ﻿  //Per ogni oggetto, valorizzo il campo di input con il valore attuale + 1
﻿  ﻿  for(var i = 0; i < divs.snapshotLength; i++) {
﻿  ﻿  ﻿  //Trovo il valore dell'oggetto all'asta
﻿  ﻿  ﻿  var match = divs.snapshotItem(i).innerHTML.match(new RegExp(": ([0-9]*\.*[0-9]*) <"));
﻿  ﻿  ﻿  //Se non trovo un valore da incrementare, non faccio nulla
﻿  ﻿  ﻿  if(!match) continue;
﻿  ﻿  ﻿  //Preparo il nuovo valore sostituendo il punto che gladiatus usa per separare le migliaia
﻿  ﻿  ﻿  var valore = match[1].replace(/\./g, "");
﻿  ﻿  ﻿  //Inserisco il valore nell'apposito campo
﻿  ﻿  ﻿  var input = XQueryFromElement(".//input[@name='bid_amount']", divs.snapshotItem(i));
﻿  ﻿  ﻿  if(input.snapshotLength) {
﻿  ﻿  ﻿  ﻿  input.snapshotItem(0).value = valore;
﻿  ﻿  ﻿  }
﻿  ﻿  }
﻿  }
}

function XQuery(query) {
    return document.evaluate(query, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}

function XQueryFromElement(query, DOMElement) {
    return document.evaluate(query, DOMElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
}

// 隱藏寶石商人
var mainmenu = document.getElementById('mainmenu').getElementsByTagName('a');
if(true) {
﻿  for(var i=0; i< mainmenu.length; i++) {
﻿  ﻿  if(mainmenu[i].innerHTML.indexOf("寶石商人") != -1) mainmenu[i].setAttribute('style','display:none');
﻿  }
}

var button=document.getElementsByTagName('input');
for(var i=0; i<button.length; i++) {
﻿  if(button[i].getAttribute('value') == '縮減時間 (10)')  //隱藏任務寶石花費按鈕
﻿  ﻿  button[i].setAttribute('style','display:none;');
﻿  else if(button[i].getAttribute('value') == '縮減等候時間')  //隱藏戰場寶石花費按鈕
﻿  ﻿  button[i].setAttribute('style','display:none;');
﻿  else if(button[i].getAttribute('value') == '進入地下城')  //隱藏地下城寶石花費按鈕
﻿  ﻿  button[i].setAttribute('style','display:none;');
﻿  else if(button[i].getAttribute('value') == '繼續遠征' || button[i].getAttribute('value') == '派遣間諜')  //隱藏遠征寶石花費按鈕
﻿  ﻿  button[i].setAttribute('style','display:none;');
﻿  else if(button[i].getAttribute('value') == '新貨上架')  //隱藏商店寶石花費按鈕
﻿  ﻿  button[i].setAttribute('style','display:none;');
﻿  else if(button[i].getAttribute('value') == '即時買斷')  //隱藏拍賣場寶石花費按鈕
﻿  ﻿  button[i].setAttribute('style','display:none;');
﻿  else if(button[i].getAttribute('value') == '縮短旅行時間') //隱藏旅行寶石花費按鈕
﻿  ﻿  button[i].setAttribute('style','display:none;');
}


// 隱藏商店中無法購買物品
hideNotAffordableObjects();
function hideNotAffordableObjects()
{
﻿  var isShopPage = urlMatch("mod=inventory.*sh=.*");
﻿  var objectTransparency = 0.25;
    if( isShopPage && unsafeWindow.res1 != null && unsafeWindow.res2 != null )
    {
﻿  ﻿  
﻿      // 取得商店中的物品
﻿      var allDivs = document.evaluate("div", document.getElementById("shop"), null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
﻿  ﻿  for (var i = 0; i < allDivs.snapshotLength; i++)
﻿      {
﻿  ﻿      var oggetto = allDivs.snapshotItem(i);
            var info = unsafeWindow.dd.elements[oggetto.id];

            if(info)
            {
                // 持有金額無法購買該物品,物品透明化
                if(info.iid && (info.preis > unsafeWindow.res1 || info.preis2 > unsafeWindow.res2)) oggetto.style.opacity = objectTransparency;
                else oggetto.style.opacity = 1;
            }
﻿      }
﻿  }
}

oldSetToolTip = unsafeWindow.SetToolTip;
unsafeWindow.SetToolTip = function (updateTooltip)
{
    if(updateTooltip)
    {
        setTimeout(hideNotAffordableObjects, 900);
    	//setTimeout(showAdditionalTooltips, 1000);
    }
	return oldSetToolTip(updateTooltip);
}

function urlMatch(string) {
﻿  var firstRE = new RegExp("http:\\/\\/s\\d+\\.gladiatus\\..*\\/game\\/index\\.php\\?"+ string,"");
﻿  var secondRE = new RegExp("http:\\/\\/s\\d+\\.\\w\\w\\.gladiatus\\..*\\/game\\/index\\.php\\?"+ string,"");
﻿  return firstRE.test(location.href) || secondRE.test(location.href);
}

// Change List
// 2011-03-23 - 0.1 - 初版建立(攻擊次數計算,本地時間,隱藏寶石商人)